package com.punai.gk.controller;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.StringUtils;
import com.punai.gk.domain.bo.GkTreeList;
import com.punai.gk.domain.vo.GkUseDataVo;
import com.punai.gk.service.IGkUseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 归口数据Controller
 * 
 * @author ruoyi
 * @date 2023-01-06
 */
@RestController
@RequestMapping("/gk/data")
public class GkUseDataController extends BaseController
{
    @Autowired
    private IGkUseDataService gkUseDataService;

//    @PreAuthorize("@ss.hasPermi('gk:data:list')")
    @GetMapping("/listtree/{templateId}")
    public AjaxResult listtree(@PathVariable("templateId") String templateId) {
        return AjaxResult.error("接口废弃");
//        List<TreeBo> list = gkUseDataService.listtree(templateId);
    }

    @GetMapping("/listGkTable/{templateId}")
    public AjaxResult listGkTable(@PathVariable("templateId") String templateId)
    {
        if (StringUtils.isEmpty(templateId)) {
            return AjaxResult.error("模板id获取失败");
        }
        List<GkTreeList> gkTreeLists = gkUseDataService.listGkTable(templateId);
        return AjaxResult.success(gkTreeLists);
    }

    @GetMapping("/listGkTableSimplify/{templateId}")
    public AjaxResult listGkTableSimplify(@PathVariable("templateId") String templateId)
    {
        List<GkTreeList> gkTreeLists = gkUseDataService.listGkTableSimplify(templateId);
        return AjaxResult.success(gkTreeLists);
    }

    /**
     * 获取归口数据详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gk:data:query')")
    @GetMapping(value = "/{templateId}/{id}")
    public AjaxResult getInfo(@PathVariable("templateId") String templateId,@PathVariable("id") String id)
    {
        return AjaxResult.success(gkUseDataService.getInfoObj(templateId,id));
    }

    /**
     * 新增归口数据
     */
    @PreAuthorize("@ss.hasPermi('gk:data:add')")
    @Log(title = "归口数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GkUseDataVo vo)
    {
        return toAjax(gkUseDataService.insertGkUseData(vo));
    }

    /**
     * 修改归口数据
     */
    @PreAuthorize("@ss.hasPermi('gk:data:edit')")
    @Log(title = "归口数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GkUseDataVo vo)
    {
        return toAjax(gkUseDataService.updateGkUseData(vo));
    }

    /**
     * 删除归口数据
     */
    @PreAuthorize("@ss.hasPermi('gk:data:operation')")
    @Log(title = "归口数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateId}/{id}")
    public AjaxResult remove(@PathVariable("templateId") String templateId,@PathVariable("id") String id)
    {
        return toAjax(gkUseDataService.deleteGkUseData(templateId,id));
    }

    @PreAuthorize("@ss.hasPermi('gk:data:operation')")
    @PostMapping("/addDataXY")
    public AjaxResult addDataXY(@RequestBody GkUseDataVo vo)
    {
        if (StringUtils.isEmpty(vo.getTemplateId())) {
            return AjaxResult.error("模板id不能为空");
        }
        return toAjax(gkUseDataService.insertGkUseDataXy(vo));
    }

    @PreAuthorize("@ss.hasPermi('gk:data:operation')")
    @PutMapping("/updateDataXY")
    public AjaxResult updateDataXY(@RequestBody GkUseDataVo vo)
    {
        return toAjax(gkUseDataService.updateGkUseDataXy(vo));
    }

    @PreAuthorize("@ss.hasPermi('gk:data:operation')")
    @PutMapping("/moveDataXY/{id}/{dropId}")
    public AjaxResult moveDataXY(@PathVariable String id,@PathVariable String dropId)
    {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        if (StringUtils.isEmpty(dropId)) {
            return AjaxResult.error("放置不能为空");
        }
        return toAjax(gkUseDataService.moveDataXY(id,dropId));
    }
}
